const app = getApp()
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    forbidData: [],
    level:0,
    levelStars:0,
    departmentRanking:0,
    blankStat:[],
    levelName:'',
    itype:'',
    timeState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      itype: app.globalData.answerType
    }) 

    let data = app.globalData.formalPapers

    this.setDateFn(data)
    this.setTimeFn(data)
  },
  
  onShow: function () {
    this.getPersonageLevel()
  },
  setRouter(e) {
    let id = e.currentTarget.dataset.id
    let icon = e.currentTarget.dataset.icon

    wx.navigateTo({
      url: `/pages/officialExplain/officialExplain?id=${id}&icon=${icon}`
    })
  },

  //初始化数据
  setDateFn(data){
    data.map((item) => {
      if (item.isParticipated == false) {
        item.type = false
        item.tipCOnt = "不可参与"
      } else if (item.startStatus == 2 || item.startStatus == 3) {
        item.type = false
        item.tipCOnt = (item.startStatus == 2 ? '对战将在' + item.startTime + '开始': '已结束')
      } else if (item.isOpen == false) {
        item.type = false
        let nearestDuration = item.nearestDuration
        let weekIarr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        item.tipCOnt = '对战将在' + `${weekIarr[nearestDuration.weekDay - 1]} ${nearestDuration.startTime}` + '开始'
      } else {
        item.type = true
      }

      return item
    })

    this.setData({
      forbidData: data.filter(item => item.type == false)
    })

    this.setData({
      listData: data.filter(item => item.type == true)
    })
  },

  viewRanking() {
    wx.navigateTo({
      url: `/pages/contests/contests`
    })
    
  },

  // 倒计时刷新
  setTimeFn(date){
    if (this.data.timeState) return;
    this.setData({
      timeState: true
    })
    let iarr = date.map(item => Date.parse(new Date(item.createTime))).sort((a, b) => a - b)
    let idata = iarr
    let newDate = Date.parse(new Date())
    let time = null
    if(newDate - (30 * 60 * 1000) <= idata) {
      let times = (newDate - idata) / 1000
      
      time = setInterval(()=>{
        times-- 
        if( times <= 0 )  {
          clearInterval(time)
          this.setData({
            timeState:false
          })

          let data = app.globalData.formalPapers
          this.setDateFn(data)
        }
      },1000)
    }
  },
  getPersonageLevel() {
    qcloud.request({
      url: config.service.obtainQualifyingPersonRanking,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      login: true,
      data: {
        'uId': app.globalData.uId,
        'rUId': app.globalData.uId
      },
      success: (response) => {
        let data = response.data
        
        if( data.code === 0 ) {
          let qualifying = data.qualifyingUserStatDTO

          this.setData({
            level: qualifying.level
          })

          this.setData({
            levelStars: Number(qualifying.levelStars)
          })

          let iarr = [1,2,3].map((item)=>{
            return item <= this.data.levelStars
          })

          this.setData({
            blankStat: iarr
          })


          this.setData({
            levelName: app.globalData.qualifyingNames[this.data.level-1]
          })

          this.setData({
            departmentRanking: qualifying.departmentRanking
          })
        }
      },
      fail: function (err) {
        console.log(err);
      }
    })
  }
  // 
})